﻿package frEngine.loaders.particleSub.particleInstance
{
    import __AS3__.vec.*;
    import com.gengine.gc.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class MeshParticle extends MeshBase
    {
        private var _byteArray:ByteArray;

        public function MeshParticle(param1:Object, param2:Function)
        {
            var _loc_3:URLLoader = null;
            super(0, 0, false, false);
            this.callBack = param2;
            this.hasLoaded = false;
            if (param1 is String)
            {
                _loc_3 = new URLLoader();
                _loc_3.load(new URLRequest(String(param1)));
                _loc_3.dataFormat = URLLoaderDataFormat.BINARY;
                _loc_3.addEventListener(IOErrorEvent.IO_ERROR, this.errorHander);
                _loc_3.addEventListener(Event.COMPLETE, this.loadComplete);
            }
            else
            {
                this.loadComplete(null, ByteArray(param1));
            }
            return;
        }// end function

        private function errorHander(event:IOErrorEvent) : void
        {
            trace("loaded error in MeshParticle.as:", event.text);
            return;
        }// end function

        private function loadComplete(event:Event, param2:ByteArray = null) : void
        {
            var _loc_5:URLLoader = null;
            if (event != null)
            {
                _loc_5 = event.currentTarget as URLLoader;
                _loc_5.removeEventListener(Event.COMPLETE, this.loadComplete);
                _loc_5.removeEventListener(IOErrorEvent.IO_ERROR, this.errorHander);
                this._byteArray = URLLoader(event.currentTarget).data;
            }
            else
            {
                this._byteArray = param2;
            }
            this._byteArray.endian = Endian.LITTLE_ENDIAN;
            this._byteArray.position = 0;
            var _loc_3:* = this._byteArray.readUnsignedShort();
            var _loc_4:* = this._byteArray.readUnsignedShort();
            if (this.checkIsCompressMesh(_loc_3, _loc_4))
            {
                this._byteArray.position = 0;
                this._byteArray.uncompress();
                _loc_3 = this._byteArray.readUnsignedShort();
                _loc_4 = this._byteArray.readUnsignedShort();
            }
            hasNormal = Boolean(_loc_3 & 1);
            useAlpha = Boolean(_loc_3 & 16);
            this.parserNormaMesh();
            this._byteArray.clear();
            this._byteArray = null;
            this.hasLoaded = true;
            if (this.callBack != null)
            {
                this.callBack.apply();
                this.callBack = null;
            }
            return;
        }// end function

        private function checkIsCompressMesh(param1:uint, param2:uint) : Boolean
        {
            var _loc_3:Boolean = false;
            if (param1 == 0 || param1 == 4096 || param1 & 1 || param1 & 16)
            {
                if (param2 == 1)
                {
                    _loc_3 = true;
                }
                else
                {
                    _loc_3 = false;
                }
            }
            else
            {
                _loc_3 = false;
            }
            return !_loc_3;
        }// end function

        private function parserNormaMesh() : void
        {
            var _loc_1:* = readString(this._byteArray);
            var _loc_2:* = readString(this._byteArray);
            hasNormal = this._byteArray.readByte() == 1 || hasNormal;
            numVertex = this._byteArray.readUnsignedShort();
            var _loc_3:* = this._byteArray.readUnsignedShort();
            var _loc_4:* = new GByteArray();
            _loc_4.endian = Endian.LITTLE_ENDIAN;
            var _loc_5:* = new GByteArray();
            _loc_5.endian = Endian.LITTLE_ENDIAN;
            sizePerVertex = 2;
            var _loc_6:* = useAlpha ? (4) : (3);
            sizePerVertex = sizePerVertex + _loc_6;
            var _loc_7:* = sizePerVertex + 3;
            sizePerVertex = sizePerVertex + 3;
            this._byteArray.readBytes(_loc_4, 0, numVertex * sizePerVertex * 4);
            this._byteArray.readBytes(_loc_5, 0, _loc_3 * 3 * 2);
            if (hasNormal)
            {
                this.vertexVector = this.changeBytesToVector(_loc_4);
            }
            else
            {
                this.vertexVector = this.changeBytesToVector(_loc_4);
            }
            this.indexVector = this.changeBytesToVectorInt(_loc_5);
            return;
        }// end function

        private function changeBytesToVectorInt(param1:ByteArray) : Vector.<uint>
        {
            var _loc_2:* = new Vector.<uint>;
            param1.position = 0;
            var _loc_3:* = param1.length / 2;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_2.push(param1.readUnsignedShort());
                _loc_4++;
            }
            return _loc_2;
        }// end function

        private function changeBytesToVector(param1:ByteArray) : Vector.<Number>
        {
            var _loc_2:* = param1.length / 4;
            param1.position = 0;
            var _loc_3:* = new Vector.<Number>;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2)
            {
                
                _loc_3.push(param1.readFloat());
                _loc_4++;
            }
            return _loc_3;
        }// end function

        public static function readString(param1:ByteArray) : String
        {
            var _loc_2:String = null;
            var _loc_6:int = 0;
            var _loc_3:int = 0;
            var _loc_4:* = param1.position;
            var _loc_5:int = 500;
            while (_loc_3 < _loc_5)
            {
                
                _loc_2 = param1.readMultiByte(1, "gb2312");
                if (_loc_2 == "\\")
                {
                    _loc_6 = param1.position - _loc_4 - 1;
                    param1.position = _loc_4;
                    _loc_2 = param1.readMultiByte(_loc_6, "gb2312");
                    param1.position = param1.position + 2;
                    break;
                }
                _loc_3++;
            }
            if (_loc_3 == _loc_5)
            {
                throw new Error("3dmax输出有误，字符串没有加\\");
            }
            return _loc_2;
        }// end function

    }
}
